/**

 * 
 */
package it.uniroma3.dia.digraph.view;

import it.uniroma3.dia.digraph.view.interpolation.NodeView;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JPanel;

import org.graphdrawing.graphml.xmlns.EdgeType;


/**
 * @author BarycentricCoords
 *
 */
public class DrawingPanel extends JPanel {

	private static final long serialVersionUID = 745292207969541953L;
	
	private List<EdgeType> edges;
	private Map<String, NodeView> id2nodes;
	private Color prevColor;
	private Color defaultNodeColor, noDefaultNodeColor;
	
	/**
	 * Creates a new DrawingPanel
	 */
	public DrawingPanel(){
		super();
		this.setBackground(Color.WHITE);
		this.defaultNodeColor = Color.BLUE;
		this.noDefaultNodeColor = Color.RED;
	}
	
	/**
	 * Draws an edge
	 * @param edge the edge to be drawn
	 * @param g the {@link Graphics} element to be used to draw the edge 
	 */
	private void drawEdge(EdgeType edge, Graphics g) {
		int sx, sy, dx, dy;
		NodeView current = this.id2nodes.get(edge.getSource());
		sx = (int)current.getX();
		sy = (int)current.getY();
		current = this.id2nodes.get(edge.getTarget());
		dx = (int)current.getX();
		dy = (int)current.getY();
		g.drawLine(sx, sy, dx, dy);	
		
		
	}
	
	/**
	 * Draws the edges previously set on the local field
	 * @param g the {@link Graphics} element to be used to draw the edges
	 */
	private void drawEdges(Graphics g) {
		prevColor = g.getColor();
		Set<String> set = new HashSet<String>();
		set.add("n4");
		set.add("n14");
		set.add("n13");
		set.add("n5");
		set.add("n20");
		set.add("n1");
		
		
		    for(EdgeType edge : this.edges) {
//			if (set.contains(edge.getSource())&& set.contains(edge.getTarget()))
			this.drawEdge(edge, g);
		    }
		
		
//		g.setColor(Color.BLUE);
//		g.drawLine(150, 200, 250, 490);
		g.setColor(prevColor);
//		System.out.println(Line2D.ptSegDist(150, 200, 250, 490, this.id2nodes.get("d").getX(), this.id2nodes.get("d").getY()));
		//TODO: traiettoria lineare del nodo
		//TODO: traiettoria curvilinea delle coord baricentriche
		
	}
	
	/**
	 * Draws the graph. Graph edges and nodes have to be previously set to the relative fields.
	 * @param g the {@link}Graphics element to be used to draw the graph on the panel
	 */
	private void drawGraph(Graphics g){	
		if(this.edges!=null){
			this.drawEdges(g);		
			this.drawNodes(g);
		}
	}

	/**
	 * Draws a node
	 * @param node the node to be drawn
	 * @param g the {@link}Graphics element to be used to draw the node
	 */
	private void drawNode(NodeView node, Graphics g) {		
		Graphics2D g2 = (Graphics2D) g;
		g2.setColor(this.defaultNodeColor); 
		Set<String> set = new HashSet<String>();
		set.add("n4");
		set.add("n14");
		set.add("n13");
		set.add("n5");
		set.add("n20");
		set.add("n1");
		
//		if (set.contains(node.getId())) {
		    if (!node.isDefault())
			g2.setColor(this.noDefaultNodeColor);

		    Ellipse2D.Double d = new Ellipse2D.Double(node.getX()-4, node.getY()-4, 8, 8);
		    //		g.fillOval((int)node.getX(), (int)node.getY(), 8,8);
		    g2.draw(d);
		    g2.drawString(node.getId(), (int)node.getX()-4, (int)node.getY()-4);
//		}
		
		//TODO: aggiustami
		
		
		
	}

	/**
	 * Draws the nodes 
	 * @param g the {@link}Graphics element to be used to draw the nodes
	 */
	private void drawNodes(Graphics g) {
		prevColor = g.getColor();
		for (NodeView node : this.id2nodes.values())
			this.drawNode(node, g);
		g.setColor(prevColor);
	}

	/**
	 * @return a {@link List} of the edges
	 */
	public List<EdgeType> getEdges() {
		return edges;
	}

	/**
	 * @return a {@link Map} that uses node ids as key and the relative node as value
	 */
	public Map<String, NodeView> getId2nodes() {
		return id2nodes;
	}

	/* (non-Javadoc)
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		this.drawGraph(g);		
	}

	/**
	 * Repaints this panel
	 * @param g the {@link}Graphics element to be used to repaint the panel
	 */
	public void repaint(Graphics g){
		super.repaint();
		this.drawGraph(g);
		
	}	

	/**
	 * @param edges
	 */
	public void setEdges(List<EdgeType> edges) {
		this.edges = edges;
	}

	/**
	 * @param id2nodes
	 */
	public void setId2nodes(Map<String, NodeView> id2nodes) {
		this.id2nodes = id2nodes;
	}
	

}
